<?php
// important check if plugin started correctly
if(!function_exists('plugins')) exit;

// articles
if(($GLOBALS['where'] == 'articles' or $GLOBALS['where'] == 'all') && $GLOBALS['query'])
{
	#tworzenie warunkow do zapytania
	$title_='title_'.$_SESSION['lang_short'];
	$text_='text_'.$_SESSION['lang_short'];
	$warunki=" (($title_ LIKE '%".$GLOBALS['src'][0]."%') or ($text_ LIKE '%".$GLOBALS['src'][0]."%'))";

	for($i = 1; $i<$GLOBALS['src_c']; $i++)
		$warunki.=" && (($title_ LIKE '%".$GLOBALS['src'][$i]."%') or ($text_ LIKE '%".$GLOBALS['src'][$i]."%'))";

	if(!$_GET['start'])
		$start = 0;
	else
		$start = $_GET['start'];

	if(!$_GET['count'])
		$count = 10;
	else
		$count = $_GET['count'];

	$db=new dbquery;
	$db->query("SELECT count(id) as rec FROM ".$GLOBALS['conf']['prefix']."article WHERE visible = 1 && ($warunki ) $cat_ids_") or $db->err(__FILE__, __LINE__);
	$cnt = $db->fetch_object();
	$cnt = $cnt->rec;
	$GLOBALS['theme_array']['result_article']=$cnt;

	$count+=100;
	$db->query("SELECT * FROM ".$GLOBALS['conf']['prefix']."article WHERE visible = 1 && ($warunki ) $cat_ids_ LIMIT $start, $count") or $db->err(__FILE__, __LINE__);
	$count-=100;

	if($cnt > 0)
	{
		$n = 0;
		$res=array();
		while($r = $db->fetch_object())
		{
			$txt=strip_exbcode($r->$text_);
			$title = 0;
			$title_odm = 0;
			$text = 0;
			$text_odm = 0;

			for($i = 0; $i<$GLOBALS['src_c']; $i++)
			{
				$title += @substr_count(strtolower($r->$title_), strtolower($GLOBALS['src'][$i]).' ');
				$title_odm += @substr_count(strtolower($r->$title_), strtolower($GLOBALS['src'][$i]));
				$text += @substr_count(strtolower($txt), strtolower($GLOBALS['src'][$i]).' ');
				$text_odm += @substr_count(strtolower($txt), strtolower($GLOBALS['src'][$i]));
			}

			$txt=add_dots_($txt, 200);

			$res[$n] = array("idn" => $r->id, "title" => $title, "title_odm" => $title_odm, "text" => $text, "text_odm" => $text_odm, "txt" => $txt, "ttl" => strip_exbcode($r->$title_), "cat"=>$r->cat, "date"=>$r->date);
			$n++;
		}

		if($n > 0)
		{
			###################################
			#sortowanie tablicy
			$t=array();$t_odm=array();$tx=array();$tx_odm=array();
			foreach($res as $key => $row)
			{
				$idn[$key] = $row["idn"];
				$t[$key] = $row["title"];
				$t_odm[$key] = $row["title_odm"];
				$tx[$key] = $row["text"];
				$tx_odm[$key] = $row["text_odm"];
			}

			#sortowanie
			array_multisort($t, SORT_DESC, $t_odm, SORT_DESC, $tx, SORT_DESC, $tx_odm, SORT_DESC, $res);

			###################################
			#wypisywanie wynikow

			$i=0;
			$GLOBALS['theme_array']['article']='';
			foreach($res as $item)
			{
				if($item['cat'])
					$link='<a href="'.cat_get_mr($item['cat'], 'articles').'">'.get_cat_name_by_id($item["cat"]).'</a>';
				else
					$link='';

				if($GLOBALS[conf]['mod_rewrite'])
					$url='article-'.read_text_mr($item['ttl']).'_'.$item['idn'].'.html';
				else
					$url='index.php?module=article&amp;id='.$item['idn'];

				for($k = 0; $k < $GLOBALS['src_c']; $k++)
				{
					$item['ttl'] = eregi_replace('('.$GLOBALS[src][$k].')', '<span class="search_string">\\1</span>', $item['ttl']);
					$item['txt'] = eregi_replace('('.$GLOBALS[src][$k].')', '<span class="search_string">\\1</span>', $item['txt']);
				}

				$item['txt'] .= string_template($lang['read_more'], array('url'=>$url));

				$GLOBALS['theme_array']['article'] .= string_template(read_file($GLOBALS['theme_path'].'search_item_article.php'), array('theme_path'=>$teme_path, 'id'=>$item['idn'], 'url'=>$url, 'link'=>$link, 'date'=>date($GLOBALS[conf]['date_format'], strtotime($item['date'])), 'i'=>($i%2+1), 'name'=>$item['ttl'], 'text'=>$item['txt']));		

				if($i+1==$count) break;
				$i++;
			}
			$GLOBALS['theme_array']['link_article']=split_to_pages('<a href="index.php?module=search&amp;query='.$GLOBALS['query'].'&amp;where=articles&amp;count='.$count.'&amp;start={rec}">{nr}</a>', $count, $GLOBALS['theme_array']['result_article'], $start);
		}
	}
	
	#dodanie do tablicy thema
	$GLOBALS['theme_array']['category_article']=NULL;
}
?>
